package dev.alm.reggietakeoutservice.utils;

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Slf4j
public class JedisUtil {

    private static volatile JedisPool jedisPool = null;

    private JedisUtil() {
    }

    public static JedisPool getJedisPoolInstance() {
        if (null == jedisPool) {
            synchronized (JedisUtil.class) {
                if (null == jedisPool) {
                    JedisPoolConfig config = new JedisPoolConfig();
                    config.setMaxTotal(200);
                    config.setMaxIdle(50);
                    config.setMaxWaitMillis(100 * 1000);
                    config.setBlockWhenExhausted(true);
                    config.setTestOnBorrow(true);
                    jedisPool = new JedisPool(config, "linux.server", 6379);
                }
            }
        }
        return jedisPool;
    }

    public static void release(Jedis jedis) {
        if (null != jedis) {
            jedis.close();
        }
    }

}
